package team.bluepen.supermarket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import team.bluepen.supermarket.entity.User;
import team.bluepen.supermarket.mapper.UserMapper;
import team.bluepen.supermarket.service.impl.utils.UserDetailsImpl;
import team.bluepen.supermarket.service.user.LoginRegisterService;
import team.bluepen.supermarket.util.JwtUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 狗崽
 */
@Service
public class LoginRegisterServiceImpl implements LoginRegisterService {
    private final AuthenticationManager authenticationManager;
    private final UserMapper userMapper;
    private final PasswordEncoder passwordEncoder;
    public LoginRegisterServiceImpl(AuthenticationManager authenticationManager,
                                    PasswordEncoder passwordEncoder,
                                    UserMapper userMapper) {
        this.authenticationManager = authenticationManager;
        this.passwordEncoder = passwordEncoder;
        this.userMapper = userMapper;
    }

    @Override
    public Map<String, String> getToken(String username, String password) {

        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(username, password);

        Authentication authenticate =
                authenticationManager.authenticate(authenticationToken);
        UserDetailsImpl loginUser = (UserDetailsImpl)
                authenticate.getPrincipal();
        User user = loginUser.getUser();
        String jwt = JwtUtil.
                createJWT(user.getId().toString());

        Map<String, String> map = new HashMap<>();
        map.put("message", "success");
        map.put("token", jwt);

        return map;
    }

    @Override
    public Map<String, String> register(String username, String password, String confirmedPassword) {
        Map<String,String> map = new HashMap<>();
        if (username ==null){
            map.put("message","用户名不能为空");
            return map;
        }
        if (password == null|| confirmedPassword == null ){
            map.put("message","密码不能为空");
            return map;
        }

        username = username.trim();
        if (username.length() == 0){
            map.put("message","用户名不能为空");
            return map;
        }

        if (password.length() == 0 || confirmedPassword.length() == 0){
            map.put("message","密码不能为空");
            return map;
        }

        if (!password.equals(confirmedPassword)){
            map.put("message","两次输入的密码不一致");
            return map;
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<User> users = userMapper.selectList(queryWrapper);
        if (!users.isEmpty()){
            map.put("message","用户名已存在");
            return map;
        }

        String EncodedPassword = passwordEncoder.encode(password);
        User user = new User(null,username,EncodedPassword);

        userMapper.insert(user);
        map.put("message","register_success");
        return map;
    }

}
